<?php
if(!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
    exit('Access Denied');
}

// 处理表单提交
if(submitcheck('configsubmit')) {
    $configs = $_POST['config'];
    foreach($configs as $key => $value) {
        if($key == 'credit') {
            // 服务器端验证最小值不能小于1
            if($value['min'] < 1) {
                cpmsg('最小值不能小于1', 'action=plugins&operation=config&do='.$pluginid.'&identifier=erling_qd&pmod=config', 'error');
                exit;
            }
            $value = json_encode(array($value['min'], $value['max']));
        }
        DB::update('er_sign_in_config', array('value' => $value), "`key`='".addslashes($key)."'");
    }
    cpmsg('设置更新成功', 'action=plugins&operation=config&do='.$pluginid.'&identifier=erling_qd&pmod=config', 'succeed');
}

$configs = DB::fetch_all("SELECT * FROM ".DB::table('er_sign_in_config'));
$credit_values = json_decode($config['value'], true);
if (!is_array($credit_values) || count($credit_values) != 2) {
    $credit_values = [1, 30]; // 默认值
}

// 处理积分类型数据
$extcredits_array = DB::fetch_all("SELECT * FROM %t WHERE skey LIKE %s", array('common_setting', 'extcredits%'));
if ($extcredits_array && isset($extcredits_array[0]['svalue'])) {
    $extcredits_data = unserialize($extcredits_array[0]['svalue']);
} else {
    $extcredits_data = [];
}
// var_dump($extcredits_data);
// exit;

$float_values = isset($configs['position_float_value']['value']) ? json_decode($configs['position_float_value']['value'], true) : [92, 35];


// 定义模板变量
$positions = [
    'cpnav_extra1' => '顶部导航栏',
    'usernav_extra3' => '用户导航栏',
    'nav_extra' => '主导航栏',
    'footerlink' => '底部链接栏',
    // 'float' => '浮动'
];
$mobile_positions = [
    'index_find_extra_mobile' => '发现', 
];

include template('erling_qd:config');